package client.adminGUI.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.adminGUI.GUIAdmin;
import client.adminGUI.UsersPanel;
import client.adminGUI.user.UserPanel;
import client.backend.user.User;
import client.backend.Connection;
import server.conn.admin.AdminThread;
import java.io.IOException;

public class EditPanel extends JPanel implements ActionListener{
	private JLabel idLabel = new JLabel("ID:");
	private JLabel user_id; 
	private JLabel unameLabel = new JLabel("Username");
	private JTextField user_uname;
	private JLabel nameLabel = new JLabel("Common Name:");
	private JTextField user_name;
	private JLabel emailLabel = new JLabel("Email:");
	private JTextField user_email;
	private JLabel leverageLabel = new JLabel("Leverage:");
	private JTextField user_leverage;
	private JLabel statusLabel = new JLabel("Status:");
	private JLabel user_status;
	private JButton saveButton;
	private JButton freezeButton;
	private JButton back;
	private User user;
	
	public static final int BOXWIDTH = 500;
	public static final int BOXHEIGHT = 300;
	public static final int LINEHEIGHT = 25;
	public static final int LEFTMARGIN = 10;
	public static final int COLWIDTH = 125;
	
	public EditPanel(User user){
		super();
		this.user=user;
		initialize();
	}
	
	public void initialize(){
		this.setLayout(null);
		this.setBackground(GUIAdmin.Background_Color);
		this.setBounds((GUIAdmin.WIDTH-BOXWIDTH)/2, (GUIAdmin.HEIGHT-BOXHEIGHT)/2, BOXWIDTH, BOXHEIGHT);
		idLabel.setBounds(LEFTMARGIN, 1*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		unameLabel.setBounds(LEFTMARGIN, 2*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		nameLabel.setBounds(LEFTMARGIN, 3*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		emailLabel.setBounds(LEFTMARGIN, 4*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		leverageLabel.setBounds(LEFTMARGIN, 5*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		statusLabel.setBounds(LEFTMARGIN, 6*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		
		user_id = new JLabel(user.getID()+"");
		user_id.setBounds(COLWIDTH, 1*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		user_uname = new JTextField(user.getUserName());
		user_uname.setBounds(COLWIDTH, 2*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		user_name = new JTextField(user.getRealName());
		user_name.setBounds(COLWIDTH, 3*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		user_email = new JTextField(user.getEMail());
		user_email.setBounds(COLWIDTH, 4*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		user_leverage = new JTextField(user.getLeverage()+"");
		user_leverage.setBounds(COLWIDTH, 5*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		user_status = new JLabel(user.isActive()? "Active":"InActive");
		user_status.setBounds(COLWIDTH, 6*LINEHEIGHT, COLWIDTH, LINEHEIGHT);
		this.add(idLabel);
		this.add(unameLabel);
		this.add(nameLabel);
		this.add(emailLabel);
		this.add(leverageLabel);
		this.add(statusLabel);
		this.add(user_id);
		this.add(user_uname);
		this.add(user_name);
		this.add(user_email);
		this.add(user_leverage);
		this.add(user_status);
		
		freezeButton = new JButton(user.isActive()? "Freeze":"UnFreeze");
		freezeButton.setBounds(LEFTMARGIN,7*LINEHEIGHT, 80, 30);
		freezeButton.addActionListener(this);
		freezeButton.setVisible(true);
		saveButton = new JButton("Save");
		saveButton.setBounds(LEFTMARGIN+100,7*LINEHEIGHT, 80, 30);
		saveButton.addActionListener(this);
		saveButton.setVisible(true);
		back = new JButton("Back");
		back.setBounds(LEFTMARGIN + 200, 7*LINEHEIGHT, 80, 30);
		back.addActionListener(this);
		back.setVisible(true);
		
		this.add(freezeButton);
		this.add(saveButton);
		this.add(back);
	}
	
	private boolean SaveInfo(){
		String leverage = user_leverage.getText().trim();
		for(int i=0;i<leverage.length();i++){
			if(!Character.isDigit(leverage.charAt(i)) && leverage.charAt(i)!='.')
				return false;
		}
		float levfloat = new Float(leverage);
		try{
			String command = AdminThread.USER_SET_INFO+AdminThread.SPLIT+user.getID()+AdminThread.SPLIT+user_uname.getText()+AdminThread.SPLIT+user_name.getText()+AdminThread.SPLIT+user_email.getText()+AdminThread.SPLIT+levfloat;
			System.out.println(command);
			Connection.getInstance().writeToServer(command);
		} catch(IOException e){
			return false;
		}
		return true;
	}
	
	public void actionPerformed(ActionEvent e) {
		GUIAdmin.getInstance().clearMessage();
    	if (e.getSource() == saveButton){
    		if(SaveInfo()) {
    			GUIAdmin.getInstance().setMessage("User information successfully updated");
    		} else
    			GUIAdmin.getInstance().setMessage("Error in user input!");
    	}
    	else if (e.getSource()==freezeButton){
    		String outcome = (user.isActive()) ? "frozen" : "unfrozen";
    		String f = (user.isActive()) ? "0" : "1";
    		String command = AdminThread.USER_FREEZE+AdminThread.SPLIT+user.getID()+AdminThread.SPLIT+f;
    		
    		try {
				Connection.getInstance().writeToServer(command);
				GUIAdmin.getInstance().setMessage("User has been "+outcome);
			} catch (IOException e1) {
				GUIAdmin.getInstance().setMessage("User could not be "+outcome);
			}
    	} else if (e.getSource() == back) {
    		UsersPanel.getInstance().refreshTable();
    		GUIAdmin.getInstance().hideUser();
    	}
    }
}
